package com.example.testdemo.demos.web.Service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.testdemo.demos.web.Config.JwtInfo;
import com.example.testdemo.demos.web.Config.Result;
import com.example.testdemo.demos.web.Config.ResultEnum;
import com.example.testdemo.demos.web.Dao.UserPurviewDao;
import com.example.testdemo.demos.web.Entity.User;
import com.example.testdemo.demos.web.Entity.UserPurview;
import com.example.testdemo.demos.web.Service.JwtRedistService;
import com.example.testdemo.demos.web.base.RedisDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;

import static com.example.testdemo.demos.web.Tool.JwtUtil.verifyTokenUid;

@Service
public class JwtRedistServiceImpl implements JwtRedistService {

    @Resource
    private RedisDao redisDao;

    @Resource
    private UserPurviewDao userPurviewDao;

    /**
     * 返回user信息
     * 和权限数组
     * @param token
     * @return
     */

    @Override
    public JwtInfo getUserInfo(String token) {
        JwtInfo jwtInfo =new JwtInfo();
        User user = (User) redisDao.vGet(verifyTokenUid(token));
        HashMap<String,Object> map =new HashMap<>();
        if (user==null || user.getUserId()==null){
            Result result =new Result();
            result.setCode(ResultEnum.AUTHORIZE_FAILURE.getCode());
            result.setMsg(ResultEnum.AUTHORIZE_FAILURE.getData());
            jwtInfo.setPass(false);
            jwtInfo.setResult(result);
        }else {
            LambdaQueryWrapper<UserPurview> lqw =new LambdaQueryWrapper<>();
            lqw.eq(UserPurview::getUserId,user.getUserId());
            String[] str =userPurviewDao.selectOne(lqw).getUserPurview().split(",");
            jwtInfo.setPass(true);
            jwtInfo.setUserId(user.getUserId());
            jwtInfo.setPurview(str);
            jwtInfo.setUser(user);

        }
        return jwtInfo;
    }

}
